package cn.ming.wfchat.app.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.AppService.SendCodeCallback;
import cn.ming.wfchat.app.main.SplashActivity;
import cn.ming.wfchat.kit.ChatManagerHolder;
import cn.ming.wfchat.kit.R;
import cn.ming.wfchat.kit.databinding.DialogLockScreenBinding;
import cn.ming.wfchat.kit.dialog.CenterDialog;
import cn.ming.wfchat.kit.net.OKHttpHelper;
import cn.wildfirechat.remote.ChatManager;
import cn.wildfirechat.remote.MD5Util;

/**
 * @author luoming
 * created at 2020-05-09 22:24
 * 锁屏弹框
 */
public class LockScreenDialog extends CenterDialog implements View.OnClickListener {
    private DialogLockScreenBinding binding;
    private String addnumber = "";


    @Override
    public int getLayoutId() {
        return R.layout.dialog_lock_screen;
    }

    public static LockScreenDialog newInstance() {
        LockScreenDialog dialog = new LockScreenDialog();
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);
        if (dialog.getWindow() != null) {
            dialog.getWindow().setDimAmount(0.65f);
        }
        ViewDataBinding binding = getLayoutBind();
        View view = binding.getRoot();
        dialog.setCancelable(isCancele());
        dialog.setCanceledOnTouchOutside(isCancele());
        dialog.setContentView(view);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        initView(binding);
        initWindowParams(dialog);
        return dialog;
    }

    @Override
    public void initView(ViewDataBinding dataBinding) {
        binding = (DialogLockScreenBinding) dataBinding;
        binding.zero.setOnClickListener(this::onClick);
        binding.one.setOnClickListener(this::onClick);
        binding.two.setOnClickListener(this::onClick);
        binding.three.setOnClickListener(this::onClick);
        binding.four.setOnClickListener(this::onClick);
        binding.five.setOnClickListener(this::onClick);
        binding.six.setOnClickListener(this::onClick);
        binding.seven.setOnClickListener(this::onClick);
        binding.eight.setOnClickListener(this::onClick);
        binding.nine.setOnClickListener(this::onClick);
        binding.delete.setOnClickListener(this::onClick);

    }

    @Override
    public boolean isBottom() {
        return true;
    }

    @Override
    public boolean isCancele() {
        return false;
    }

    @Override
    public void show(Object object) {
        super.show(object);
        addnumber = "";
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.zero) {
            add("0");
        } else if (id == R.id.one) {
            add("1");
        } else if (id == R.id.two) {
            add("2");
        } else if (id == R.id.three) {
            add("3");
        } else if (id == R.id.four) {
            add("4");
        } else if (id == R.id.five) {
            add("5");
        } else if (id == R.id.six) {
            add("6");
        } else if (id == R.id.seven) {
            add("7");
        } else if (id == R.id.eight) {
            add("8");
        } else if (id == R.id.nine) {
            add("9");
        } else if (id == R.id.delete) {
            delete();
        }
    }

    public void add(String addStr) {
        addnumber = addnumber + addStr;
        initPassword();
        if (addnumber.length() == 6) {
            if (TextUtils.equals(MD5Util.getMD5String(addnumber), ChatManager.getDestroyPassword()) && ChatManager.getDestroyPasswordEnable()) {
                //等于销毁密码
                validateLockPassword();
            } else if (TextUtils.equals(MD5Util.getMD5String(addnumber), ChatManager.getLockPassword())) {
                dismissAllowingStateLoss();
            } else {
                ChatManager.showToast("密码错误");
                addnumber = "";
                initPassword();
            }
        }
    }

    public void delete() {
        if (addnumber.length() > 0) {
            addnumber = addnumber.substring(0, addnumber.length() - 1);
            initPassword();
        }
    }

    private void initPassword() {
        binding.tv6.setText("");
        binding.tv5.setText("");
        binding.tv4.setText("");
        binding.tv3.setText("");
        binding.tv2.setText("");
        binding.tv1.setText("");
        switch (addnumber.length()) {
            case 6:
                binding.tv6.setText("●");
            case 5:
                binding.tv5.setText("●");
            case 4:
                binding.tv4.setText("●");
            case 3:
                binding.tv3.setText("●");
            case 2:
                binding.tv2.setText("●");
            case 1:
                binding.tv1.setText("●");
                break;
            default:
                break;
        }

    }

    /**
     * 销毁账号
     */
    void validateLockPassword() {
        AppService.Instance().validateDestroyPassword(MD5Util.getMD5String(addnumber), new SendCodeCallback() {
            @Override
            public void onUiSuccess() {
                dismissAllowingStateLoss();
                //不要清除session，这样再次登录时能够保留历史记录。如果需要清除掉本地历史记录和服务器信息这里使用true
                ChatManagerHolder.gChatManager.disconnect(true, true);
                SharedPreferences sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
                sp.edit().clear().apply();
                OKHttpHelper.clearCookies();
                Intent intent = new Intent(getActivity(), SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                getActivity().finish();
            }

            @Override
            public void onUiFailure(String msg) {
                ChatManager.showToast(msg);
            }
        });
    }

}
